package com.tongtech.system.controller;

import com.tongtech.common.core.constant.UserConstants;
import com.tongtech.common.core.utils.StringUtils;
import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.ResourceStructure;
import com.tongtech.system.service.IResourceStructureService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源体系信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/resourceStructure")
public class ResourceStructureController extends BaseController
{
    @Autowired
    private IResourceStructureService resourceStructureService;

    /**
     * 获取资源体系列表
     */
    @RequiresPermissions("basicdata:resource-structure:list")
    @GetMapping("/list")
    public AjaxResult list(ResourceStructure resourceStructure)
    {
        List<ResourceStructure> resourceStructures = resourceStructureService.selectResourceStructureList(resourceStructure);
        return success(resourceStructures);
    }

    /**
     * 查询资源体系列表（排除节点）
     */
    @RequiresPermissions("basicdata:resource-structure:list")
    @GetMapping("/list/exclude/{resourceStructureId}")
    public AjaxResult excludeChild(@PathVariable(value = "resourceStructureId", required = false) Long resourceStructureId)
    {
        List<ResourceStructure> resourceStructures = resourceStructureService.selectResourceStructureList(new ResourceStructure());
        resourceStructures.removeIf(p -> p.getResourceStructureId().intValue() == resourceStructureId || ArrayUtils.contains(StringUtils.split(p.getAncestors(), ","), resourceStructureId + ""));
        return success(resourceStructures);
    }

    /**
     * 根据资源体系编号获取详细信息
     */
    @RequiresPermissions("basicdata:resource-structure:query")
    @GetMapping(value = "/{resourceStructureId}")
    public AjaxResult getInfo(@PathVariable Long resourceStructureId)
    {
        return success(resourceStructureService.selectResourceStructureById(resourceStructureId));
    }

    /**
     * 新增资源体系
     */
    @RequiresPermissions("basicdata:resource-structure:add")
    @Log(title = "资源体系管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResourceStructure resourceStructure)
    {
        if (!resourceStructureService.checkResourceStructureNameUnique(resourceStructure))
        {
            return error("新增资源体系'" + resourceStructure.getResourceStructureName() + "'失败，资源体系名称已存在");
        }
        resourceStructure.setCreateBy(SecurityUtils.getUsername());
        return toAjax(resourceStructureService.insertResourceStructure(resourceStructure));
    }

    /**
     * 修改资源体系
     */
    @RequiresPermissions("basicdata:resource-structure:edit")
    @Log(title = "资源体系管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ResourceStructure dept)
    {
        Long deptId = dept.getResourceStructureId();
        if (!resourceStructureService.checkResourceStructureNameUnique(dept))
        {
            return error("修改资源体系'" + dept.getResourceStructureName() + "'失败，资源体系名称已存在");
        }
        else if (dept.getParentId().equals(deptId))
        {
            return error("修改资源体系'" + dept.getResourceStructureName() + "'失败，上级资源体系不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && resourceStructureService.selectNormalChildrenResourceStructureById(deptId) > 0)
        {
            return error("该资源体系包含未停用的子资源体系！");
        }
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(resourceStructureService.updateResourceStructure(dept));
    }

    /**
     * 删除资源体系
     */
    @RequiresPermissions("basicdata:resource-structure:remove")
    @Log(title = "资源体系管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId)
    {
        if (resourceStructureService.hasChildByResourceStructureId(deptId))
        {
            return warn("存在下级资源体系,不允许删除");
        }
        return toAjax(resourceStructureService.deleteResourceStructureById(deptId));
    }
}
